<template>
  <div style="padding: 10px 15px">
    <!-- 查询条件-->
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="请输入查询内容"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item seachbttn"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查询</el-button>
      <el-button
        v-if="Jurisdiction('添加')"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >新增</el-button>
      <el-button
        v-if="Jurisdiction('删除')"
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="deleteClick"
      >删除</el-button>
    </div>
    <div class="filter_fenge" style="border:none;border-top:1px solid #d1ddeb" />
    <div class="filter-container">
      <el-button
        v-if="Jurisdiction('添加')"
        class="filter-item bttnleft"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >新增</el-button>
      <el-button
        v-if="Jurisdiction('删除')"
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="deleteClick"
      >删除</el-button>
    </div>
    <!--查询条件 end-->

    <!--table-->
    <el-table
      v-loading="listLoading"
      :data="MngrLogData"
      fit
      highlight-current-row
      style="width: 100%"
      stripe
    >
      <el-table-column label="操作IP" align="left" width="130px">
        <template slot-scope="{ row }">
          <span>{{ row.ipAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录名" align="left" width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.loginName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="left" width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="left" width="165px">
        <template slot-scope="{ row }">
          <span>{{ row.createTimeStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="left" width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.titles }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作内容" align="left">
        <template slot-scope="{ row }">
          <span>{{
            row.contents.length > 200
              ? row.contents.substring(0, 200) + "......"
              : row.contents
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleClick(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData(this)"
    />

    <!--table end -->

    <!--弹出层  新增-->
    <el-dialog title="详情" :visible.sync="dialogFormVisible" width="55%">
      <el-form :model="form" :inline="true" label-position="right">
        <el-form-item label="操作内容">
          <el-input
            v-model="form.contents"
            type="textarea"
            style="width:100%;min-width:200px;"
          />
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--弹出层 end-->
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { AjaxToken, TipConfirm } from '@/api/common'
import { SelectAjax } from '@/api/common'
import { Jurisdiction } from '@/api/common'
import sessionStorage from '@/api/Storage'
import 'url-search-params-polyfill' // 兼容IE，否则无法识别URLSearchParams
export default {
  name: 'MngrLog',
  components: { Pagination },
  data() {
    return {
      logincpnid: '',
      multipleSelection: [],
      formLabelWidth: '120px',
      MngrLogData: null,
      listQuery: {
        title: '',
        total: 0,
        page: 1,
        limit: 10
      },
      listLoading: true,
      dialogFormVisible: false,
      dialogdetailsVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      form: {
        iD: '',
        uId: '',
        createTime: '',
        contents: '',
        ipAddress: '',
        isDeleted: '',
        titles: ''
      },
      details_form: {
        iD: '',
        uId: '',
        createTime: '',
        contents: '',
        ipAddress: '',
        isDeleted: '',
        titles: ''
      }
    }
  },

  // 初始化
  created() {
    var userinfo = JSON.parse(sessionStorage.getItem('userinfo'))
    this.logincpnid = userinfo.cpnId
    this.fetchData()
  },
  methods: {
    // 弹层关闭
    handleClose() {},
    // 权限
    Jurisdiction(moduleActionName) {
      return Jurisdiction(moduleActionName, this)
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 多选赋值
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    // 新增
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.form = {
        iD: '',
        uId: '',
        createTime: '',
        contents: '',
        ipAddress: '',
        isDeleted: '',
        titles: ''
      }
    },
    // 编辑
    handleClick(row) {
      this.form.iD = row.iD
      this.form.uId = row.uId
      this.form.createTime = row.createTime
      this.form.contents = row.contents
      this.form.ipAddress = row.ipAddress
      this.form.isDeleted = row.isDeleted
      this.form.titles = row.titles
      this.dialogStatus = 'edit'
      this.dialogFormVisible = true
    },
    // 保存
    saveForm() {
      var arr = this.multipleSelection
      var params = new URLSearchParams()
      params.append('UId', this.form.uId)
      params.append('CreateTime', this.form.createTime)
      params.append('Contents', this.form.contents)
      params.append('IpAddress', this.form.ipAddress)
      params.append('IsDeleted', this.form.isDeleted)
      params.append('Titles', this.form.titles)
      backstage.SaveMngrLog(this, params)
    },
    // 删除
    deleteClick(row) {
      var multis = []
      var params = new URLSearchParams()
      if (row == undefined || row == '') {
        var arr = this.multipleSelection
        if (arr == undefined || arr.length <= 0) {
          this.$message({
            showClose: true,
            message: '请至少选中一行',
            type: 'error'
          })
          this.listLoading = false
          return
        } else {
          for (var i = 0; i < arr.length; i++) {
            multis.push(arr[i].id)
          }
          params.append('delds', JSON.stringify(multis))
        }
      } else {
        multis.push(row.id)
        params.append('delds', JSON.stringify(multis))
      }
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      })
        .then((a) => {
          this.listLoading = true
          backstage.DelMngrLog(this, params)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          this.listLoading = false
        })
    },
    // 查询
    fetchData() {
      this.listLoading = true
      var params = new URLSearchParams()
      params.append(
        'Parms',
        '{"page":' +
          this.listQuery.page +
          ',"rows":' +
          this.listQuery.limit +
          '}'
      )
      var user = JSON.parse(sessionStorage.getItem('userinfo'))
      // params.append("CpnID",this.form.cpnID == "" ? this.user.cpnId : this.form.cpnID);
      params.append('Contents', this.listQuery.title)
      params.append('CpnID', this.logincpnid)
      backstage.GetMngrLogLst(this, params)
    }
  }
}
// 后台事件
var backstage = {
  // 获取MngrLog集合
  GetMngrLogLst: function(thisobj, params) {
    AjaxToken(
      thisobj,
      'GetMngrLogLst',
      params,
      'post',
      function(res) {
        thisobj.listLoading = false
        var body = res.data.data
        thisobj.MngrLogData = []
        thisobj.MngrLogData = body.rows
        thisobj.listQuery.page = body.page
        thisobj.listQuery.total = body.records
      },
      function(err) {
        thisobj.listLoading = false
      }
    )
  },
  SaveMngrLog: function(thisobj, params) {
    AjaxToken(
      thisobj,
      'SaveMngrLog',
      params,
      'post',
      function(res) {
        var body = res.data
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: 'success',
            duration: 2000
          })
          thisobj.fetchData()
          thisobj.dialogFormVisible = false
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: 'error'
          })
        }
      },
      function(err) {}
    )
  },
  DelMngrLog: function(thisobj, params) {
    AjaxToken(
      thisobj,
      'DelMngrLog',
      params,
      'post',
      function(res) {
        thisobj.listLoading = false
        var body = res.data
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: 'success',
            duration: 2000
          })
          thisobj.fetchData()
          thisobj.dialogFormVisible = false
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: 'error'
          })
        }
      },
      function(err) {}
    )
  }
}
</script>
<style lang="scss" scoped>
.el-textarea__inner {
  height: 400px !important;
}
</style>
